Practical-09
Mobile Application
Practical List
Create an android program that will play a media file from the memory card.
Steps
- Create a new Android project in Android Studio.
- Open the
activity_main.xmllayout file. - Add a
Buttonelement to the layout with the following attributes:android:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Play Media File"android:layout_gravity="center"
- Open the
MainActivity.javafile. - Inside the
onCreatemethod, add the following code to set the content view to theactivity_main.xmllayout:setContentView(R.layout.activity_main); - Create a new method called
playMediaFileto handle the button click event and play the media file:private void playMediaFile() {
String filePath = "/path/to/media/file.mp3"; // Replace with the actual file path
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
} - Inside the
onCreatemethod, add the following code to set the click listener for the button:Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playMediaFile();
}
}); - Run the application on an Android device or emulator.
- Click the "Play Media File" button to play the media file from the memory card.
- Verify that the media file is played successfully.